<?php

namespace app\admin\model\kuerp;

use think\Model;

class Goods extends Model
{

    // 表名
    protected $name = 'kuerp_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'more_unit_text',
        'is_offten_text',
        'status_text'
    ];
    

    
    public function getMoreUnitList()
    {
        return ['1' => __('More_unit 1'), '2' => __('More_unit 2')];
    }

    public function getIsOfftenList()
    {
        return ['0' => __('Is_offten 0'), '1' => __('Is_offten 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getMoreUnitTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['more_unit']) ? $data['more_unit'] : '');
        $list = $this->getMoreUnitList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsOfftenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_offten']) ? $data['is_offten'] : '');
        $list = $this->getIsOfftenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function goodstype()
    {
        return $this->belongsTo('GoodsType', 'goods_type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function unitconversion()
    {
        return $this->belongsTo('UnitConversion', 'more_unit_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function unit()
    {
        return $this->belongsTo('Unit', 'unit_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 设置查询条件
     * @param array $where 查询条件
     * @param string $dbName 数据库表名
     * @return array
     */
    public function setWhere($where=[],$dbName='')
    {
//        $admin = \think\Session::get('admin');
//        if(isset($admin['id'])){
//            if(!empty($dbName)){
//                $where[$dbName.'.admin_id'] = $admin['id'];
//            }else{
//                $where['admin_id'] = $admin['id'];
//            }
//        }
        return $where;
    }

    /**
     * 获取单位成本
     * @param string $goods_code 商品编码
     * @param string $supplyer_code 供应商编码
     * @param string $now_nmber 当前数量
     * @param array $purchase_info 采购信息
     * @return array
     */
    public function getCost_Price($goods_code,$supplyer_code='',$now_nmber='',$purchase_info=[]){
        //获取设置
        $set= \think\Db::name('kuerp_sets')->where($this->setWhere())->field('type,name,value,is_checked,set')->select();
        $setList = [];
        foreach ($set as $set_k=>$set_v){
            if(!empty($set_v['set'])){
                $set_v['set'] = unserialize($set_v['set']);
            }
            $setList[$set_v['name']] = $set_v;
        }
        //数量价格格式化
        $price_decimal  = $setList['price_decimal']['value'];

        $taxe = \think\Db::name('kuerp_suppliers')->where($this->setWhere(['supplyer_code'=>$supplyer_code]))->value('vat_rate');
        if($taxe!==0){
            if(empty($taxe)){
                $taxe = \think\Db::name('kuerp_sets')->where($this->setWhere(['name'=>'taxes']))->value('value');
            }
        }
        $set= \think\Db::name('kuerp_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();
        $price_taxes= \think\Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked');

        $goods = \think\Db::name('kuerp_goods')->where($this->setWhere(['goods_code'=>$goods_code]))->find();
        //期初
        $now_nmbers = (float)$now_nmber;$now_all_money = 0;
        $number = 1;

        $valuation_set= \think\Db::name('kuerp_sets')->where(['name'=>'valuation'])->value('value');//计算方法
        if($valuation_set!=2){
            $valuation_set = 1;
        }
        $now_nmbers2 = 0;

        while ($now_nmber>0){//循环计算并可预先扣除后一天的成本
            $number++;
            foreach ($purchase_info as $k=>$v){
                if($valuation_set==1){
                    if($now_nmber>0){
                        $now_nmbers2 += $v['basic_nmber'];
                        $now_nmber -= $v['basic_nmber'];
                        $now_all_money += $v['all_money'];
                        $purchase_info[$k]['basic_nmber'] = 0;
                        $purchase_info[$k]['all_money'] = 0;
                        if($now_nmber>=$v['basic_nmber']){
                            $purchase_info[$k]['basic_nmber'] = 0;
                            $purchase_info[$k]['all_money'] = 0;
                        }else{
                            $all_unit = round(($now_all_money/$now_nmbers2)*$now_nmber,4);
                            $purchase_info[$k]['basic_nmber'] -= $now_nmber;
                            $purchase_info[$k]['all_money'] -= $all_unit;
                            $now_nmber = 0;
                        }
                    }
                }else{
                    if($now_nmber>0){
                        $now_nmbers2 += $v['basic_nmber'];
                        $now_nmber -= $v['basic_nmber'];
                        $now_all_money += $v['all_money'];
                        $purchase_info[$k]['basic_nmber'] = 0;
                        $purchase_info[$k]['all_money'] = 0;
                        if($now_nmber>=$v['basic_nmber']){
                            $purchase_info[$k]['basic_nmber'] = 0;
                            $purchase_info[$k]['all_money'] = 0;
                        }else{
                            $all_unit = round(($now_all_money/$now_nmbers2)*$now_nmber,4);
                            $purchase_info[$k]['basic_nmber'] -= $now_nmber;
                            $purchase_info[$k]['all_money'] -= $all_unit;
                            $now_nmber = 0;
                        }
                    }
                }
                if($now_nmber<=0){
                    break;
                }
            }
            if($now_nmber>0){
                break;
            }
        }
        if($valuation_set==1){
            $now_nmbers = $now_nmbers2;
        }
        if($now_nmbers!=0){
            $cost = ($now_all_money)/($now_nmbers);//成本
        }else{
            $cost = $goods['purchase'];
            if($set['is_checked']==1){
                if($price_taxes==1){
                    $cost = $cost*100/(100+$taxe);
                }
            }
        }
        return ['cost'=>$cost,'purchase_info'=>$purchase_info];
    }
}
